from PyQt5.QtWidgets import (QHBoxLayout, QLabel, QPushButton,QLineEdit, QMessageBox)
from components.actiondialog import ActionDialog

class HotkeyDialog(ActionDialog):
    def __init__(self, action=None, parent=None):
        super().__init__(parent)

        self.action = action if action else {"type": "hotkey", "keys": []}

        # 按键输入
        key_layout = QHBoxLayout()
        key_layout.addWidget(QLabel("按下组合键:"))
        self.key_edit = QLineEdit("+".join(self.action.get("keys", [])))
        key_layout.addWidget(self.key_edit)

        # 提示
        hint = QLabel("例如: ctrl+alt+delete")
        hint.setStyleSheet("color: gray;")

        # 按钮
        button = QPushButton("确定")
        button.clicked.connect(self.accept)

        self.layout.addLayout(key_layout)
        self.layout.addWidget(hint)
        self.layout.addWidget(button)

    def accept(self):
        keys = self.key_edit.text().split("+")
        if not keys or not all(keys):
            QMessageBox.warning(self, "警告", "必须输入有效的组合键")
            return

        self.action = {
            "type": "hotkey",
            "keys": keys,
            "desc": f"组合按键: {'+'.join(keys)}"
        }
        super().accept()
